import WebAPI from "../api/index.js"

function gotoLogin() {
	let token = null
	uni.getStorage({
		key: 'token',
		success: async res => {},
		fail: async res => {
			return
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					// 获取登录凭证
					console.log(loginRes, 'loginRes');
					var code = loginRes.code;
					uni.request({
						url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxdf1aed9fae95eaeb',
						method: 'GET',
						data: {
							secret: 'c5e00be6866b3da6cab242eeb300e6a2', //你的小程序的secret,  
							code: loginRes.code //wx.login 登录成功后的code  
						},
						success: (cts) => {
							console.log(cts, 'ccccccccccccc');
						}
					});


					uni.request({
						url: WebAPI.Login,
						method: 'POST',
						data: {
							jsCode: code,
							appId: "hdb01"
						},
						success: async function(res) {
							// 处理服务器端返回的数据
							uni.setStorage({
								key: 'token',
								data: res.data.data.token,
								success: function() {
									console.log('success');
								}
							});
							uni.setStorage({
								key: 'hasPhone',
								data: res.data.data.hasPhone,
								success: function() {
									console.log('success');
								}
							});
							uni.login({
								provider: 'weixin',
								success: (loginRes) => {
									uni.getUserInfo({
										provider: 'weixin',
										success: (
											userInfoRes
										) => {
											uni.request({
												url: WebAPI
													.getUserPhone,
												method: 'POST',
												data: {
													wxCode: loginRes
														.code,
												},
												header: {
													Authorization: "Bearer:" +
														res
														.data
														.data
														.token // 修改这里的连接方式
												},
												success: (
													res
												) => {
													// 获取手机号成功
													console
														.log(
															res
														);
												}
											});
										}
									});
								}
							});
						},
						fail: function(err) {
							console.error('请求失败', err);
						}
					});
				},
				fail: function(error) {
					console.error('登录失败', error);
				}
			});


		}
	});

}

module.exports = {
	gotoLogin
}